<template>
  <comcard>
    <div slot="com_header">添加商品</div>
    <div slot="com_con">
      <el-row>
        <el-col :xs="24" :sm="8">
          <el-form label-width="100px" :model="goods">
            <el-form-item label="商品名称">
              <el-input type="text" v-model="goods.name"></el-input>
            </el-form-item>
            <el-form-item label="商品分类">
              <el-select
                v-model="categorySelection"
                placeholder="请选择"
                @change="changeSelectedValue"
              >
                <el-option
                  v-for="item in newType"
                  :key="item.cateName"
                  :label="item.cateName"
                  :value="item.cateName"
                >
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="商品价格">
              <el-input-number
                @change="handleChange"
                :min="1"
                :max="999"
                label="描述文字"
                v-model="goods.price"
              ></el-input-number>
            </el-form-item>
            <el-form-item label="商品图片：">
              <el-upload
                class="avatar-uploader"
                action="http://127.0.0.1:5000/goods/goods_img_upload"
                :show-file-list="false"
                :on-success="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload"
              >
                <img v-if="imageUrl" :src="imageUrl" class="avatar" />
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </el-form-item>

            <el-form-item label="商品描述">
              <el-input
                type="textarea"
                :rows="2"
                placeholder="请输入内容"
                v-model="goods.goodsDesc"
              >
              </el-input>

            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="add()">提交</el-button>
              <el-button>重置</el-button>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
    </div>
  </comcard>
</template>

<script>
import comcard from "../../components/card";
import { api_add, api_queryType } from "../../api/goods.js";
export default {
  components: {
    comcard,
  },
  data() {
    return {
      categorys: [],
      imageUrl: "",
      goods: {
        name: "",
        category: "",
        price: "",
        imgUrl: "	",
        goodsDesc: "",
      },
      categorySelection: "",
      newType: [],
    };
  },
  mounted() {
    this.query();
  },
  methods: {
    //上传图片

    handleChange(value) {},
    //选择seleted
    changeSelectedValue() {
      console.log(this.categorySelection);
      this.goods.category = this.categorySelection;
    },
    async query() {
      let rst = await api_queryType(this.categorys);
      this.newType = rst.categories;
      console.log(this.newType);
    },

    async add() {
      // const data = {
      //   name: this.goods.name,
      //   category: this.goods.category,
      //   price: this.goods.price,
      //   imgUrl: this.goods.imgUrl,
      //   goodsDesc: this.goods.goodsDesc,
      // };
      let rst = await api_add(this.goods);
      this.$router.replace("./list");
      // console.log(data);
      console.log(rst);
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = `http://localhost:5000/upload/imgs/goods_img/${res.imgUrl}`;
      this.goods.imgUrl = res.imgUrl;
      console.log(res, file);
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
  },
};
</script>

<style lang="less" scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>